import { defineStore } from 'pinia';
import { Session } from '@/utils/storage';
import Cookies from 'js-cookie';

const useCommonStore = defineStore('common', {
	state: () => {
		return {
			// 主题
			theme: '#409EFF',
			screen: 3, //屏幕大小 3 2 1 0 3（>1200）是大屏
			sidebar: {
				collapse: Session.get('sidebarCollapse') || false, //侧边栏否折叠
				hide: false,
				withoutAnimation: false,
			},
			labelTimeLen: undefined, //echarts图间隔
		};
	},
	actions: {
		setScreen(n: number) {
			this.screen = n;
		},
		setCollapse(flag: boolean) {
			this.sidebar.collapse = flag;
			Session.set('sidebarCollapse', flag);
		},
		toggleSideBar(withoutAnimation?: boolean) {
			this.sidebar.withoutAnimation = withoutAnimation || false;
			if (this.sidebar.collapse) {
				Cookies.set('sidebarStatus', '1');
			} else {
				Cookies.set('sidebarStatus', '0');
			}
		},
		closeSideBar(withoutAnimation: boolean) {
			Cookies.set('sidebarStatus', '0');
			this.sidebar.collapse = true;
			this.sidebar.withoutAnimation = withoutAnimation;
		},
		setLabelTimeLen(len: any) {
			this.labelTimeLen = len;
		},
	},
	// 持久化
	persist: false,
});

export default useCommonStore;
